
#include <TVout.h>
#include <fontALL.h>
#include "scoreboard.h"
#include "tv_functions.h"
#include "zuino.h"
#include "zrtc.h"
#include <I2cMaster.h>

TVout TV;
ScoreBoard SB;
ZManager ZM;

// PIN 7 and 9 for TVout video
// PIN 11      for TVout audio 
ZButton reverse_BUTTON = ZButton(2);     // +/-
ZButton home_BUTTON = ZButton(12);       // Points for Home (use reverse)
ZButton guest_BUTTON = ZButton(5);       // Points for Guest (use reverse)
ZButton arrow_BUTTON = ZButton(4);       // Switch arrow (use reverse for bouns 4 config: -- B- -B BB)
ZButton time_BUTTON = ZButton(8);        // Start and Stop time (use reverse for period)
ZButton adjusttime_BUTTON = ZButton(10); // Adjust time (use reverse)
ZLed time_LED = ZLed(6);                 // Led for time

void setup() 
{ 
  //Serial.begin(4800);
  //Serial.println("started...");
  TV.begin(PAL,120,96);  
  //tv_presentation(TV); 
    
  tv_scoreboard_background(TV);   
  tv_scoreboard_refresh(TV, SB);

  //SB.set_funct_external_mills(my_mills);
  SB.set_funct_external_mills(my_mills_rtc);
  
  SB.set_func_time_ticker(time_ticker);
  SB.set_func_time_ended(time_ended);  
  
  adjusttime_BUTTON.set_enable_long_pression(true);
  time_LED.set_enable_blinking(true);
  
  ZM.add(&reverse_BUTTON);
  ZM.add(&home_BUTTON);
  ZM.add(&guest_BUTTON);
  ZM.add(&arrow_BUTTON);
  ZM.add(&time_BUTTON);
  ZM.add(&adjusttime_BUTTON);
  ZM.add(&time_LED);
} 
  
void loop() 
{  
  ZM.refresh();
  SB.refresh();
  if(SB.need_refresh())
  {
    tv_scoreboard_refresh(TV, SB);
    SB.values_read();
  }
  
  if(home_BUTTON.pressed())
  {
    if(reverse_BUTTON.pressing())
      SB.sub_home_score();
    else
      SB.add_home_score();
  }
  
  if(guest_BUTTON.pressed())
  {    
    if(reverse_BUTTON.pressing())
      SB.sub_guest_score();
    else
      SB.add_guest_score();
  } 
 
  if(arrow_BUTTON.pressed())
  {
    if(reverse_BUTTON.pressing())
      SB.change_bonus();
    else
      SB.change_arrow();
  } 

  if(time_BUTTON.pressed())
  {
    if(reverse_BUTTON.pressing())
      SB.change_period();
    else
    {
      time_LED.blink_for(10);
      SB.stop_or_start_time();
    }
  }   

  if(adjusttime_BUTTON.pressed() || adjusttime_BUTTON.long_pressing())
  {
    if(!SB.time_running())
    {
      if(reverse_BUTTON.pressing())
        SB.sub_time();
      else
        SB.add_time();
    }
    else
    {
      if(reverse_BUTTON.pressing())
      {
        tv_sfdb(TV);
        SB.set_need_refresh();
      }
    }
  }   
}

void time_ticker()
{ 
  time_LED.blink_for(10);        
}

void time_ended()
{ 
  time_LED.blink_for(1000);
  tv_horn(TV);
}

/*
unsigned long my_mills()
{
    double FACTOR = 2.245; // 2.24 leggerente lento -> andrebbe aumentato
    unsigned long current_time_millis;
    cli();  // disable interrupts
    current_time_millis = millis();
    sei();  // enable interrupts 
    return current_time_millis * FACTOR;  
}
*/

unsigned long my_mills_rtc()
{
  uint8_t r[8];
  if (readDS1307(0, r, 8))
  {  
    char buffer [2];

    //seconds    
    sprintf(buffer, "%x%x", r[0] >> 4, r[0] & 0XF);
    int s = atoi(buffer);    
    
    //minutes
    sprintf(buffer, "%x%x", r[1] >> 4, r[1] & 0XF);
    int m = atoi(buffer);    
    
    //hours
    sprintf(buffer, "%x%x", r[2] >> 4, r[2] & 0XF);
    int h = atoi(buffer);

    //day
    sprintf(buffer, "%x%x", r[4] >> 4, r[4] & 0XF);
    int dd = atoi(buffer);
    
    return (((((dd*24UL)+h)*60UL)+m)*60UL+s)*1000UL;  
  }
  else
  {
    return 0;
  }
}
